//
//  LastestWaitCell.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/15.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "LastestWaitCell.h"
#import "MZTimerLabel.h"

@interface LastestWaitCell ()
{
    
    __weak IBOutlet UILabel *nameLab;
//    __weak IBOutlet UILabel *timeLab;
    __weak IBOutlet UIImageView *pdcImg;
    __weak IBOutlet UIImageView *clockImg;
    UILabel *timeLab;
    LatestCellModel *_currentModel;
    //倒计时
    MZTimerLabel *timerExample1;
    
}
@end
@implementation LastestWaitCell

- (void)awakeFromNib {
    // Initialization code
    clockImg.frame = CGRectMake(CGRectGetMinX(nameLab.frame), clockImg.frame.origin.y, clockImg.frame.size.width, clockImg.frame.size.height);
    
    [self addSubview:[WYPublic getFenGexian:0 :self.frame.size.height - 1 :WYMWITH :0.5]];
    self.selectionStyle = UITableViewCellSelectionStyleGray;
}
- (void)fillCellWhithModel:(LatestCellModel*)model Index:(NSInteger)index
{
    _currentModel = model;
    _index = index;
    
    //不知道下面移除三个按钮干嘛用
    //xib 描述文件里没有这个tag 111 222 333
    
    UILabel *lasttime = (UILabel *)[self viewWithTag:111];
    [lasttime removeFromSuperview];
    UIView *tiv = (UIView *)[self viewWithTag:222];
    [tiv removeFromSuperview];
    UIView *tivm = (UIView *)[self viewWithTag:333];
    [tivm removeFromSuperview];
    
    // 下面是添加显示剩余时间的label
    [pdcImg sd_setImageWithURL:[NSURL URLWithString:model.pdc_img_url] placeholderImage:[UIImage imageNamed:@"moren"]];
    nameLab.text = [NSString stringWithFormat:@"(第%@期)%@",model.pdc_qishu,model.pdc_title];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    timeLab = [[UILabel alloc]initWithFrame:CGRectMake(125*app.autoSizeScaleX, 50, 150, 34)];
    timeLab.backgroundColor = [UIColor clearColor];
    timeLab.textColor = QBB_RED_COLOR;
    timeLab.tag = 111;
    timeLab.font = [UIFont systemFontOfSize:34];
    [self addSubview:timeLab];
    /**
     *  timeLab 是现实跳动的数字
     */
    
    //时间到了显示开奖信息 jxLab就是显示的控件
    if (_currentModel.pdc_real_time == 0) {
        [timeLab removeFromSuperview];
        UILabel *jxLab = timeLab = [[UILabel alloc]initWithFrame:CGRectMake(125*app.autoSizeScaleX, 50, 150, 34)];
        jxLab.backgroundColor = [UIColor clearColor];
        jxLab.textColor = QBB_RED_COLOR;
        jxLab.tag = 333;
        jxLab.font = [UIFont systemFontOfSize:29];
        jxLab.text = @"正在开奖";
        [self addSubview:jxLab];
        
//        时间到了那么通知controller刷新这个控件
//        self.father.freshCount = 0;
        [self.father reloadVCIndex:index];
        }
    else{
            //这里才是显示倒计时的闪烁的时钟
            //倒计时处理
        //用的MZTimerLabel显示跳跃的时间
            timerExample1 = [[MZTimerLabel alloc] initWithLabel:timeLab andTimerType:MZTimerLabelTypeTimer];
            timeLab.tag = 222;
            [timerExample1 setCountDownTime:model.pdc_real_time];
            timerExample1.resetTimerAfterFinish = YES;
            timerExample1.timeFormat = @"mm:ss:SS";
        if(![timerExample1 counting]){
            
            __weak LastestWaitCell *wself = self;
           
            
            [timerExample1 startWithEndingBlock:^(NSTimeInterval countTime) {
                
                if (_currentModel.pdc_real_time == 0) {
                    [timeLab removeFromSuperview];
                    UILabel *jxLab = timeLab = [[UILabel alloc]initWithFrame:CGRectMake(125*app.autoSizeScaleX, 50, 150, 34)];
                    jxLab.backgroundColor = [UIColor clearColor];
                    jxLab.textColor = QBB_RED_COLOR;
                    jxLab.tag = 333;
                    jxLab.font = [UIFont systemFontOfSize:29];
                    jxLab.text = @"正在开奖";
                    [wself addSubview:jxLab];
                    [wself addSubview:timeLab];
                    
                    //开奖后刷新这个cell的数据
                    wself.father.freshCount = 0;
                    [wself.father reloadVCIndex:index];
                }
            }];
            
            }
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)prepareForReuse {
    [super prepareForReuse];
    if (timerExample1 != nil && [timerExample1 counting]) {
        [timerExample1 pause];
    }
}

@end
